﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Data;
using System.Web.UI.WebControls;

public partial class Catalogs_SystemConfig : SessionPageBase
{
	protected void Page_Load(object sender, EventArgs e)
	{
		CustomControlsNS.CustomButton cc = new CustomControlsNS.CustomButton(Resources.DatabaseStrings.btn_Save, Resources.DatabaseStrings.btn_Save, "$get('" + btnSave.ClientID + "').click();", "1", true);
		cc.ID = "btnSave";
		this.Master.PageToolbarButtons.Add(cc);

		btnClose.OnClick = "$get('" + CancelButton1.ClientID + "').click();";

		if (!IsPostBack)
		{
			ClearSession("ds_SystemConfig");
		}
		Load_Data(false);
	}

	private void Load_Data(bool bForce)
	{
		DataSet ds_SystemConfig = GetSessionDataSet("ds_SystemConfig");
		if (ds_SystemConfig == null || bForce)
		{
            ds_SystemConfig = Hipocrate.DB.DBConnection.GetData("System_Config_Select", new string[] { }, new object[] { });

			foreach (DataTable dt in ds_SystemConfig.Tables)
				foreach (DataColumn dc in dt.Columns)
				{
					dc.ReadOnly = false;
					dc.AllowDBNull = true;
				}

			SetSession("ds_SystemConfig", ds_SystemConfig);
			Session["ds_SystemConfig"] = null;
		}
		if (ds_SystemConfig != null && (!IsPostBack || bForce))
		{
			DataView dv0 = ds_SystemConfig.Tables[0].DefaultView;

			grSystemConfig.SessionKey = GetSessionKey("ds_SystemConfig");
			grSystemConfig.SessionKeyIndex = 0;
			grSystemConfig.SessionKeyFilter = dv0.RowFilter;

			grSystemConfig.DataSource = dv0;
			grSystemConfig.DataBind();
			grSystemConfig.CaptionForXLS = Resources.DatabaseStrings.menu_SystemConfig;
		}
	}

	protected void btnSave_Click(object sender, EventArgs e)
	{
		try
		{
			DataSet ds_SystemConfig = GetSessionDataSet("ds_SystemConfig");
			if (ds_SystemConfig != null)
			{
                Hipocrate.DB.DBConnection.SaveAll(ds_SystemConfig,
					new object[] { 
						new object[] { "System_Config_Insert", new string[] { "Code" , "Name" } }
					},
					new object[] { 
						new object[] { "System_Config_Update", new string[] { "ID", "Code", "Name" } }
					},
					new object[] { 
						new object[] { "System_Config_Delete", new string[] { "ID" } }
					}
					);
				this.Master.SetMessage(Resources.DatabaseStrings.DocumentSaved2, false);
				Load_Data(true);
			}
		}
		catch (Exception ex)
		{
			this.Master.SetMessage(ex.Message, true);
		}
	}

	protected void btnEdit_Click(object sender, EventArgs e)
	{
		dvSystemConfig.ChangeMode(DetailsViewMode.Edit);

		ImageButton btnDetails = sender as ImageButton;
		GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
		DataView dv = new DataView((GetSessionDataSet("ds_SystemConfig")).Tables[0]);
		dv.RowFilter = "ID = " + Convert.ToString(this.grSystemConfig.DataKeys[row.RowIndex].Value);

		this.dvSystemConfig.DataSource = dv;
		this.dvSystemConfig.DataBind();

		btnAdd.Visible = false;
		btnSave2.Visible = true;
		btnDelete.Visible = true;
		up2.Update();
		mdlPopup.Show();
		lblUser.Text = null;
	}

	protected void btnInsert_Click(object sender, EventArgs e)
	{
		dvSystemConfig.ChangeMode(DetailsViewMode.Insert);

		DataView dv = new DataView((GetSessionDataSet("ds_SystemConfig")).Tables[0]);
		this.dvSystemConfig.DataSource = dv;
		this.dvSystemConfig.DataBind();

		dvSystemConfig.Rows[0].Visible = false;

		btnAdd.Visible = true;
		btnSave2.Visible = false;
		btnDelete.Visible = false;
		up2.Update();
		mdlPopup.Show();
		lblUser.Text = null;
	}

	protected void btnSave2_Click(object sender, EventArgs e)
	{
		if (((TextBox)dvSystemConfig.Rows[1].Cells[1].Controls[0]).Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_code;
			return;
		}
		if (((TextBox)dvSystemConfig.Rows[2].Cells[1].Controls[0]).Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_name;
			return;
		}
		if (((TextBox)dvSystemConfig.Rows[2].Cells[1].Controls[0]).Text.Length > 100)
		{
			lblUser.Text = Resources.DatabaseStrings.length_name;
			return;
		}
		string id = dvSystemConfig.DataKey.Value.ToString();
		DataView dv = new DataView((GetSessionDataSet("ds_SystemConfig")).Tables[0]);
		dv.RowFilter = "ID=" + id;

		dv[0]["Code"] = Tools.GetSQLValue(((TextBox)dvSystemConfig.Rows[1].Cells[1].Controls[0]).Text);
		dv[0]["Name"] = Tools.GetSQLValue(((TextBox)dvSystemConfig.Rows[2].Cells[1].Controls[0]).Text);

		grSystemConfig.DataBind();
		mdlPopup.Hide();
	}

	protected void btnAdd_Click(object sender, EventArgs e)
	{
		if (((TextBox)dvSystemConfig.Rows[1].Cells[1].Controls[0]).Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_code;
			return;
		}
		if (((TextBox)dvSystemConfig.Rows[2].Cells[1].Controls[0]).Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_name;
			return;
		}
		if (((TextBox)dvSystemConfig.Rows[2].Cells[1].Controls[0]).Text.Length > 100)
		{
			lblUser.Text = Resources.DatabaseStrings.length_name;
			return;
		}
		DataView dv = new DataView((GetSessionDataSet("ds_SystemConfig")).Tables[0]);
		DataRowView drv = dv.AddNew();

		drv["Code"] = Tools.GetSQLValue(((TextBox)dvSystemConfig.Rows[1].Cells[1].Controls[0]).Text);
		drv["Name"] = Tools.GetSQLValue(((TextBox)dvSystemConfig.Rows[2].Cells[1].Controls[0]).Text);
		drv.EndEdit();

		grSystemConfig.DataBind();
		mdlPopup.Hide();
	}

	protected void btnDelete_Click(object sender, EventArgs e)
	{
		string id = dvSystemConfig.DataKey.Value.ToString();
		DataView dv = new DataView((GetSessionDataSet("ds_SystemConfig")).Tables[0]);
		dv.RowFilter = "ID=" + id;
		dv[0].Row.Delete();

		grSystemConfig.DataBind();
		mdlPopup.Hide();
	}

	protected bool ShowAssoc(string strID)
	{
		DataSet ds_SystemConfig = GetSessionDataSet("ds_SystemConfig");
		DataView dv0 = new DataView(ds_SystemConfig.Tables[0]);
		dv0.RowFilter = "ID = " + strID;
		return (dv0[0].Row.RowState != DataRowState.Added);
	}
}
